package com.utils.conmponentbase.base.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.chad.library.adapter.base.module.BaseLoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.utils.conmponentbase.R;
import com.utils.conmponentbase.base.viewmodel.BaseViewModel;

import java.util.List;
import java.util.Objects;

/**
 * MvvmDemo com.utils.conmponentbase.base.fragment
 * 2020-01-04 17:38
 */
public abstract class BaseListFragment<Entity, Binding extends ViewDataBinding, VM extends BaseViewModel> extends BaseFragment<Binding, VM> implements OnRefreshListener, OnLoadMoreListener {
    protected int page = 1;
    protected int pageSize = 10;
    protected boolean isRefresh = false;
    protected SmartRefreshLayout mRefreshLayout;
    protected BaseQuickAdapter<Entity, ? extends BaseViewHolder> mQuickAdapter;

    @Override
    public void initData() {
        super.initData();
        if (null != mRefreshLayout) {
            mRefreshLayout.setOnRefreshListener(this);
            if (isRefresh) mRefreshLayout.autoRefresh();
            else loadListData(isRefresh, mRefreshLayout, page, pageSize, this::loadCallback);
        }
        if (null != mQuickAdapter) {
            //设置adapter 可以同时存在头布局和空布局
            mQuickAdapter.setHeaderWithEmptyEnable(true);
            BaseLoadMoreModule loadMoreModule = mQuickAdapter.getLoadMoreModule();
            if (null != loadMoreModule) {
                loadMoreModule.setEnableLoadMore(true);
                loadMoreModule.setAutoLoadMore(true);
                loadMoreModule.setEnableLoadMoreIfNotFullPage(true);
                //预加载位置
                loadMoreModule.setPreLoadNumber(3);
                loadMoreModule.setOnLoadMoreListener(this);
            }
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        isRefresh = true;
        if (null != mQuickAdapter) {
            Objects.requireNonNull(mQuickAdapter.getLoadMoreModule()).setEnableLoadMore(false);
        }
        loadListData(isRefresh, mRefreshLayout, page, pageSize, this::loadCallback);
    }

    @Override
    public void onLoadMore() {
        page++;
        isRefresh = false;
        if (null == mQuickAdapter) return;
        if (mQuickAdapter.getData().size() < pageSize) {
            Objects.requireNonNull(mQuickAdapter.getLoadMoreModule()).loadMoreEnd(false);
        } else {
            Objects.requireNonNull(mQuickAdapter.getLoadMoreModule()).setEnableLoadMore(true);
            loadListData(isRefresh, mRefreshLayout, page, pageSize, this::loadCallback);
        }
    }

    protected void loadCallback(List<Entity> pList) {
        if (null == pList) {
            if (null != mQuickAdapter && null != mQuickAdapter.getLoadMoreModule()) {
                if (isRefresh) mRefreshLayout.finishRefresh();
                //TODO 数据加载失败  这地方框架(BRVAH)还有问题,等待修复
                mQuickAdapter.getLoadMoreModule().loadMoreFail();
            }
            return;
        }
        if (isRefresh) {
            mRefreshLayout.finishRefresh();
            mQuickAdapter.setNewData(pList);
//            if (null != mQuickAdapter && null != mQuickAdapter.getLoadMoreModule()) {
//                //刷新开启上拉加载
//                mQuickAdapter.getLoadMoreModule().setEnableLoadMore(true);
//            }
        } else {
            if (null != mQuickAdapter && null != mQuickAdapter.getLoadMoreModule()) {
                mQuickAdapter.addData(pList);
                if (pList.size() >= pageSize) {
                    //本次数据加载完成
                    mQuickAdapter.getLoadMoreModule().loadMoreComplete();
                } else {
//                    //加载结束后关闭上拉加载功能
//                    mQuickAdapter.getLoadMoreModule().setEnableLoadMore(false);
                    //所有数据加载结束
                    mQuickAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }
        }
        if (null != mQuickAdapter && mQuickAdapter.getData().size() <= 0) {
            View emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_empty, null);
            TextView tvEmpty = emptyView.findViewById(R.id.tv_empty);
            tvEmpty.setOnClickListener(v -> loadListData(isRefresh, mRefreshLayout, page, pageSize, this::loadCallback));
            Objects.requireNonNull(mQuickAdapter).setEmptyView(emptyView);
        }
    }

    /**
     * 加载数据
     *
     * @param isRefresh true 刷新 false 加载更多
     * @param layout    头部布局
     * @param page      页数
     * @param pageSize  每页数量
     */
    public abstract void loadListData(boolean isRefresh, Object layout, int page, int pageSize, LoadDataCallBack<Entity> pCallBack);

    /**
     * 没有数据时，加载空布局
     *
     * @return 空布局View
     */
    protected View empty() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_empty, null);
        //如果需要点击空白页做操作，在这里进行 findViewById(...)
        return view;
    }

    public interface LoadDataCallBack<E> {
        void loadCallback(List<E> pList);
    }
}
